package cn;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Locale;
import java.util.Objects;

/**
 * description:
 * author:zcx
 * lenovo
 * 时间：2022.01.17.21.07
 */
public class Kh {
//    public static void main(String[] args) throws FileNotFoundException {
//        String path = "E:\\Petsystem";
//        BufferedReader kh = new BufferedReader(new FileReader(path));
//        System.out.println(kh.lines().filter(e->e.trim().length()>0).count());
//    }
    //统计Java文件多少行
    public static long rows=0;
    public static long filecount=0;
    public static int space=0;
    public static void main(String[] args) throws FileNotFoundException {


        count(new File("D:\\zcx"));
        System.out.println(rows);
        System.out.println(filecount);
        System.out.printf("有%d%n空行",space);
    }
    public static void count(File file) throws FileNotFoundException {
        if(file.isDirectory()){
            for(File t: Objects.requireNonNull(file.listFiles())){
                if(t.isDirectory()){
                    count(t);
                }else if(t.isFile()&& t.getName().toLowerCase(Locale.ROOT).endsWith(".java"));{
                    rows+=rows(t);
                    BufferedReader kh= new BufferedReader(new FileReader(t));
                    space+=kh.lines().filter(e->e.trim().length()==0).count();
                    System.out.printf("%s(%d)[%3$tF %<tT]%n", t.getAbsolutePath(), rows(t), t.lastModified());
                    ++filecount;
                }
            }
        }else if(file.getName().endsWith(".java")){
            rows+=rows(file);
            BufferedReader kh= new BufferedReader(new FileReader(file));
            space+=kh.lines().filter(e->e.trim().length()==0).count();
            System.out.printf("%s(%d)[%3$tF %<tT]%n", file.getAbsolutePath(), rows(file), file.lastModified());
            ++filecount;
        }
    }
    public static long rows(File file){
        long rows = 0;
        try {
            var br = new BufferedReader(new FileReader(file));
            rows = br.lines().count();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rows;
    }
}
